package com.xuelang.pipeline.entry.msg;

import com.xuelang.pipeline.app.msg.MsgUserMessagesService;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.msg.UserMessageDTO;
import com.xuelang.pipeline.client.msg.UserMessageQueryDTO;
import com.xuelang.pipeline.client.msg.UserMessageReadMsgDTO;
import com.xuelang.pipeline.client.msg.UserMessageSaveDTO;
import com.xuelang.sys.app.sys.config.JadpConfiguration;
import com.xuelang.sys.app.sys.utils.UserContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 用户消息管理
 */
@Slf4j
@RestController
@RequestMapping("/msg")
public class UserMessageController {

    @Autowired
    private MsgUserMessagesService userMessagesService;

    @Autowired
    JadpConfiguration jadpConfiguration;


    /**
     * 保存消息
     * @param userMessageSaveDTO
     * @return
     */
    @PostMapping(value = "/saveMessage")
    public UserMessageSaveDTO saveMessage(@RequestBody UserMessageSaveDTO userMessageSaveDTO) {
        if (jadpConfiguration.getEnableDebugTestData()) {
            UserMessageSaveDTO userMessageSaveDTO1 = userMessagesService.saveMessage(userMessageSaveDTO);
            return userMessageSaveDTO1;
        }
        return null;
    }


    /**
     * 分页查询用户消息
     *
     * @param userMessageQueryDTO
     * @return
     */
    @PostMapping(value = "/findUserMessagePageList")
    public PageRecord<UserMessageDTO> findUserMessagePageList(@RequestBody UserMessageQueryDTO userMessageQueryDTO) {
        if (!jadpConfiguration.getEnableDebugTestData()) {
            userMessageQueryDTO.setReceiveUserId(UserContextUtils.getUserId());
        }
        PageRecord<UserMessageDTO> userMessagePageList = userMessagesService.findUserMessagePageList(userMessageQueryDTO);
        return userMessagePageList;
    }

    /**
     * 标记读取用户某条消息
     *
     * @param readMsgDTO
     * @return
     */
    @PostMapping(value = "/userTagsReadMessages")
    public List<UserMessageDTO> userTagsReadMessages(@RequestBody UserMessageReadMsgDTO readMsgDTO) {
        if (!jadpConfiguration.getEnableDebugTestData()) {
            readMsgDTO.setReceiveUserId(UserContextUtils.getUserId());
        }
        List<UserMessageDTO> userMessageDTOS = userMessagesService.userTagsReadMessages(readMsgDTO);
        return userMessageDTOS;
    }

    /**
     * 查询用户未读消息数量
     *
     * @param readMsgDTO
     * @return
     */
    @PostMapping(value = "/getUserUnReadMessageCount")
    public Map<String,Long> getUserUnReadMessageCount(@RequestBody UserMessageReadMsgDTO readMsgDTO) {
        if (!jadpConfiguration.getEnableDebugTestData()) {
            readMsgDTO.setReceiveUserId(UserContextUtils.getUserId());
        }
        Long msgCount = userMessagesService.getUserUnReadMessageCount(readMsgDTO);
        Map<String,Long> res = Collections.singletonMap("msgCount",msgCount);
        return res;
    }



}
